#######################################
#  diff in diff - varying css data (only replicate main tables)
#######################################




css2<- read.csv("css07_cv.csv")
css2$part<- css2$cash + css2$vouchers
css2$part[css2$part==2]<-1
css2$part<- css2$part*100
css2_1<- read.csv("css09_cv.csv")
css2_1$part<- css2_1$cash + css2_1$vouchers
css2_1$part[css2_1$part==2]<-1
css2_1$part<- css2_1$part*100
css2_2<- read.csv("css11_cv.csv")
css2_2$part<- css2_2$cash + css2_2$vouchers
css2_2$part[css2_2$part==2]<-1
css2_2$part<- css2_2$part*100
css2_3<- read.csv("css13_cv.csv")
css2_3$part<- css2_3$cash + css2_3$vouchers
css2_3$part[css2_3$part==2]<-1
css2_3$part<- css2_3$part*100
css2_4<- read.csv("css15_cv.csv")
css2_4$part<- css2_4$cash + css2_4$vouchers
css2_4$part[css2_4$part==2]<-1
css2_4$part<- css2_4$part*100


###################################

# indiv d-i-d 

######## 
css2$income<- as.numeric(paste0(substr(css2$income,2,nchar(css2$income))))
css2_1$income<- as.numeric(paste0(substr(css2_1$income,2,nchar(css2_1$income))))
css2_2$income<- as.numeric(paste0(substr(css2_2$income,2,nchar(css2_2$income))))
css2_3$income<- as.numeric(paste0(substr(css2_3$income,2,nchar(css2_3$income))))
css2_4$income<- as.numeric(paste0(substr(css2_4$income,2,nchar(css2_4$income))))

css2$income_perc<- ceiling(ecdf(css2$income)(css2$income)*10)
css2_1$income_perc<- ceiling(ecdf(css2_1$income)(css2_1$income)*10)
css2_2$income_perc<- ceiling(ecdf(css2_2$income)(css2_2$income)*10)
css2_3$income_perc<- ceiling(ecdf(css2_3$income)(css2_3$income)*10)
css2_4$income_perc<- ceiling(ecdf(css2_4$income)(css2_4$income)*10)


css2_1$voucher_yr<-0
css2_1$voucher_yr[css2_1$elec_year>2016]<-1
css2$voucher_yr<-0
css2$voucher_yr[css2$elec_year>2016]<-1
css2_2$voucher_yr<-0
css2_2$voucher_yr[css2_2$elec_year>2016]<-1
css2_3$voucher_yr<-0
css2_3$voucher_yr[css2_3$elec_year>2016]<-1
css2_4$voucher_yr<-0
css2_4$voucher_yr[css2_4$elec_year>2016]<-1

css2$republican<- 0
css2$republican[css2$party=="Democratic"]<- 0
css2$republican[css2$party=="Republican"]<- 1
css2$republican[css2$party=="Non-Partisan"]<- 0
css2_1$republican<- 0
css2_1$republican[css2_1$party=="Democratic"]<- 0
css2_1$republican[css2_1$party=="Republican"]<- 1
css2_1$republican[css2_1$party=="Non-Partisan"]<- 0
css2_2$republican<- 0
css2_2$republican[css2_2$party=="Democratic"]<- 0
css2_2$republican[css2_2$party=="Republican"]<- 1
css2_2$republican[css2_2$party=="Non-Partisan"]<- 0
css2_3$republican<- 0
css2_3$republican[css2_3$party=="Democratic"]<- 0
css2_3$republican[css2_3$party=="Republican"]<- 1
css2_3$republican[css2_3$party=="Non-Partisan"]<- 0
css2_4$republican<- 0
css2_4$republican[css2_4$party=="Democratic"]<- 0
css2_4$republican[css2_4$party=="Republican"]<- 1
css2_4$republican[css2_4$party=="Non-Partisan"]<- 0

css2$iswhiteperson<- 0
css2$iswhiteperson[css2$race=="European"]<- 1
css2$iswhiteperson[is.na(css2$race)]<- NA
css2$iswhiteperson[css2$race==""]<- NA
css2_1$iswhiteperson<- 0
css2_1$iswhiteperson[css2_1$race=="European"]<- 1
css2_1$iswhiteperson[is.na(css2_1$race)]<- NA
css2_1$iswhiteperson[css2_1$race==""]<- NA
css2_2$iswhiteperson<- 0
css2_2$iswhiteperson[css2_2$race=="European"]<- 1
css2_2$iswhiteperson[is.na(css2_2$race)]<- NA
css2_2$iswhiteperson[css2_2$race==""]<- NA
css2_3$iswhiteperson<- 0
css2_3$iswhiteperson[css2_3$race=="European"]<- 1
css2_3$iswhiteperson[is.na(css2_3$race)]<- NA
css2_3$iswhiteperson[css2_3$race==""]<- NA
css2_4$iswhiteperson<- 0
css2_4$iswhiteperson[css2_4$race=="European"]<- 1
css2_4$iswhiteperson[is.na(css2_4$race)]<- NA
css2_4$iswhiteperson[css2_4$race==""]<- NA


css1<- read.csv("css05_cv.csv")
css1$part<- css1$cash + css1$vouchers
css1$part[css1$part==2]<-1
css1$voucher_yr<-0
css1$voucher_yr[css1$elec_year>2016]<-1

#########################################
#get estimates for tables a20-a25, by year
#########################################

# 2007
qqq<- felm(part~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq1<- felm(part~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq2<- felm(part~  income_perc*voucher_yr+as.factor(elec_year)|as.factor(id)|0|0 , data=css2)
qqq3<- felm(part~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)
qqq5<- felm(part~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2)

#2009
qqq_1<- felm(part~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 ,
             data=css2_1)
qqq1_1<- felm(part~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_1)
qqq2_1<- felm(part~  income_perc*voucher_yr+as.factor(elec_year)|as.factor(id)|0|0 , data=css2_1)
qqq3_1<- felm(part~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_1)
qqq5_1<- felm(part~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_1)

#2011
qqq_2<- felm(part~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 ,
             data=css2_2)
qqq1_2<- felm(part~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_2)
qqq2_2<- felm(part~  income_perc*voucher_yr+as.factor(elec_year)|as.factor(id)|0|0 , data=css2_2)
qqq3_2<- felm(part~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_2)
qqq5_2<- felm(part~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_2)

#2013
qqq_3<- felm(part~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 ,
             data=css2_3)
qqq1_3<- felm(part~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_3)
qqq2_3<- felm(part~  income_perc*voucher_yr+as.factor(elec_year)|as.factor(id)|0|0 , data=css2_3)
qqq3_3<- felm(part~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_3)
qqq5_3<- felm(part~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_3)

#2015
qqq_4<- felm(part~iswhiteperson*voucher_yr + income_perc*voucher_yr+sums*voucher_yr+republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 ,
             data=css2_4)
qqq1_4<- felm(part~iswhiteperson*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_4)
qqq2_4<- felm(part~  income_perc*voucher_yr+as.factor(elec_year)|as.factor(id)|0|0 , data=css2_4)
qqq3_4<- felm(part~sums*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_4)
qqq5_4<- felm(part~republican*voucher_yr|as.factor(id)+as.factor(elec_year)|0|0 , data=css2_4)

#get SE using summary(model, robust=T) as below 
summary(qqq5, robust=T)


stargazer(qqq, qqq_1,qqq_2,qqq_3,qqq_4,df=F, no.space = T,single.row = F,
          dep.var.labels = c("Likelihood of donating"), digits = 2,omit.table.layout = "n",
          star.cutoffs = NA) #ta25
stargazer(qqq1, qqq1_1,qqq1_2,qqq1_3,qqq1_4,df=F, no.space = T,
          single.row = F, dep.var.labels = c("Likelihood of donating"), 
          digits = 2,omit.table.layout = "n",star.cutoffs = NA) #ta20
stargazer(qqq2, qqq2_1,qqq2_2,qqq2_3,qqq2_4,df=F, no.space = T,
          single.row = F, dep.var.labels = c("Likelihood of donating"), digits = 2,
          omit.table.layout = "n",star.cutoffs = NA) #ta23
stargazer(qqq3, qqq3_1,qqq3_2,qqq3_3,qqq3_4,df=F, no.space = T,single.row = F, 
          dep.var.labels = c("Likelihood of donating"), digits = 2,omit.table.layout = "n",
          star.cutoffs = NA) #ta24
stargazer(qqq5, qqq5_1,qqq5_2,qqq5_3,qqq5_4,df=F, no.space = T,single.row = F, 
          dep.var.labels = c("Likelihood of donating"), digits = 2,omit.table.layout = "n",
          star.cutoffs = NA) #ta22




# the remaining results are ta21
qqq4_a<- felm(part~age_at_elecyr*voucher_yr|as.factor(elec_year)|0|0 , data=css1[css1$age_at_elecyr<70,])
qqq4<- felm(part~age_at_elecyr*voucher_yr|as.factor(elec_year)|0|0 , data=css2[css2$age_at_elecyr<70,])
qqq4_1<- felm(part~age_at_elecyr*voucher_yr|as.factor(elec_year)|0|0 , data=css2_1[css2_1$age_at_elecyr<70,])
qqq4_3<- felm(part~age_at_elecyr*voucher_yr|as.factor(elec_year)|0|0 , data=css2_3[css2_3$age_at_elecyr<70,])
qqq4_4<- felm(part~age_at_elecyr*voucher_yr|as.factor(elec_year)|0|0 , data=css2_4[css2_4$age_at_elecyr<70,])
summary(qqq4_a, robust=T)
summary(qqq4, robust=T)
summary(qqq4_1, robust=T)
summary(qqq4_3, robust=T)
summary(qqq4_4, robust=T)



